import Reflux from "reflux"
import GUtils from "@/utils";
import BaseAction from "@/base/BaseAction";

const prefix = "/system/menu/"
const api = {
    search: prefix + "search",
    create: prefix + "create",
    edit: prefix + "edit",
    delete: prefix + "delete",
    getDetail: prefix + "detail",
}

const createAction = () => {
    const Action = Reflux.createActions({
        ...BaseAction,
        search: {asyncResult: true},
        getDetail: {asyncResult: true},
    });

    Action.search.listen(function (so) {
        GUtils.jsonAjax.post(api.search, so).then(this.completed, this.failed)
    })
    Action.create.listen(function (vo) {
        GUtils.jsonAjax.post(api.create, vo).then(this.completed, this.failed)
    })
    Action.edit.listen(function (vo) {
        GUtils.jsonAjax.post(api.edit, vo).then(this.completed, this.failed)
    })
    Action.delete.listen(function (id) {
        GUtils.Ajax.post(api.delete, {id}).then(this.completed, this.failed)
    })
    Action.getDetail.listen(function (id, callback) {
        GUtils.Ajax.post(api.getDetail, {id}).then(this.completed.bind(this,callback), this.failed)
    })
    return Action;
};

export {api, createAction}